package com.fingard.dsp.bank.directbank.lsbank01;

import java.util.HashMap;
import java.util.Map;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;

/**
 * Created by aaron on 2018/4/4.
 */
public class LSBANK01Balance extends LSBANK01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        //从批量的数据中依次读取单笔数据循环查询
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        String opname ="enterpriseQry";
        try {
            while (p_req7506.readRecord()) {
                RetAtsBalDetail detail = new RetAtsBalDetail();
                //非公有参数
                Map<String, String> params = new HashMap<String, String>();
                params.put("accountNo", p_req7506.readedDetail.actNum);
                params.put("currencyType", convertCurToBank(p_req7506.readedDetail.actCur));
                String[] bankResult = sendToBank(params, p_req7506.reqHeader, opname);
                if(bankResult[0].length()==0){
                    StringBuilder tmpSb =  new StringBuilder();
                    XmlTextReader tmpBkReader =  loadBankRespXmlReader(bankResult[1],tmpSb);
                        if(tmpBkReader!=null){
                        String hostReturnCode = getHostReturnCode(tmpBkReader);
                            if(isSuccess(hostReturnCode)){
                                if (tmpBkReader.read("/BankData/opRep/RepParam")) {
                                    detail.respCode = "0";
                                    detail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleTextNotNull("currencyType"));
                                    detail.actNum = tmpBkReader.readedXElement.getSingleTextNotNull("accountNo");
                                    detail.availBal = removePrefix(tmpBkReader.readedXElement.getSingleTextNotNull("balanceAvailable"));
                                    detail.frozenBal = removePrefix(tmpBkReader.readedXElement.getSingleTextNotNull("balanceFreeze"));
                                    detail.currBal = removePrefix(tmpBkReader.readedXElement.getSingleTextNotNull("balance"));
                                    detail.respInfo = getHostErrorMessage(tmpBkReader);
                                }
                            }else{
                                detail.respCode="-1";
                                detail.respInfo=getHostErrorMessage(tmpBkReader);
                            }
                        }else{
                        detail.respCode="-1";
                        detail.respInfo = tmpSb.toString();
                    }
                }else {
                    detail.respCode="-1";
                    detail.respInfo = bankResult[1];
                }
                retAts7506BalQry.writeRetDetail(detail,p_req7506.isBatch);
            }
        }catch (Exception e){
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
